package com.learn.blog.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.learn.blog.admin.dao.mapper.AdminMapper;
import com.learn.blog.admin.dao.mapper.PermissionMapper;
import com.learn.blog.admin.model.params.PageParam;
import com.learn.blog.admin.pojo.Admin;
import com.learn.blog.admin.pojo.Permission;
import com.learn.blog.admin.vo.PageResult;
import com.learn.blog.admin.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package: com.learn.blog.admin.service
 * @ClassName: AdminService
 * @Author: 陈威
 * @CreateTime: 2021/9/14 21:37
 * @Description:
 */
@SuppressWarnings("all")
@Service
public class AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    PermissionMapper permissionMapper;

    public Admin findAdminByUserName(String username) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, username);
        queryWrapper.last("limit 1");
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }

    public List<Permission> findPermissionByAdminId(Long adminId) {
        List<Permission> list = permissionMapper.findPermissionByAdminId(adminId);
        return list;
    }

    //查询用户分页结果
    public Result userList(PageParam pageParam) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(pageParam.getQueryString())) {
            queryWrapper.like(Admin::getUsername, pageParam.getQueryString());
        }
        Page<Admin> adminPage = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        Page<Admin> selectPage = adminMapper.selectPage(adminPage, queryWrapper);
        PageResult<Admin> pageResult = new PageResult<>();
        pageResult.setList(selectPage.getRecords());
        pageResult.setTotal(selectPage.getTotal());
        return Result.success(pageResult);
    }
}